package com.platform.netty.tcp.task;

import com.platform.common.config.ApplicationContextProvider;
import com.platform.common.manager.AsyncManager;
import com.platform.common.utils.netty.NettyUtil;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.common.utils.spring.SpringUtils;
import com.platform.module.domain.EqAlarm;
import com.platform.module.domain.ValveRecord;
import com.platform.module.service.IEqAlarmService;
import com.platform.module.service.ILinkConcentratorMeterService;
import com.platform.module.service.ILinkConcentratorValveService;
import com.platform.module.service.IValveRecordService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.TimerTask;

@Data
@Slf4j
public class ValvePoolTask {

    private static IValveRecordService valveRecordService;

    private static RedisUtil redisUtil;

    public static TimerTask saveValve(final String datas, final Date createTime)
    {
        valveRecordService = SpringUtils.getBean(IValveRecordService.class);

        redisUtil = SpringUtils.getBean(RedisUtil.class);

        return new TimerTask() {
            @Override
            public void run() {
                String[] contents = datas.split(" ");
                //阀id
                String valveId = contents[2] + contents[1];
                log.info("阀门id:" + valveId);
                try {
                    //阀温度上限
                    String max = NettyUtil.get10HexNum(contents[3]) + "." + NettyUtil.get10HexNum(contents[4]);
                    log.info("阀温度上限:" + max);
                    //阀温度下限
                    String min = NettyUtil.get10HexNum(contents[5]) + "." + NettyUtil.get10HexNum(contents[6]);
                    log.info("阀温度下限:" + min);
                    //阀温度值
                    String temp = NettyUtil.get10HexNum(contents[7]) * 0.5 + "";
                    log.info("阀温度值:" + temp);
                    //室内温度
                    String indoor = NettyUtil.get10HexNum(contents[10]) + "." + NettyUtil.get10HexNum(contents[9]);
                    log.info("室内温度:" + indoor);
                    //工作时间
                    String work = NettyUtil.get10HexNum(contents[13]) + NettyUtil.get10HexNum(contents[14]) + NettyUtil.get10HexNum(contents[15]) + "";
                    log.info("工作时间:" + work);
                    //阀开度
                    String open = NettyUtil.get10HexNum(contents[34]) + "";
                    log.info("阀开度:" + open);

                    ValveRecord valveRecord = new ValveRecord();
                    valveRecord.setValveId(valveId);
                    valveRecord.setMax(max);
                    valveRecord.setMin(min);
                    valveRecord.setTemp(temp);
                    valveRecord.setIndoor(indoor);
                    valveRecord.setWorkTime(work);
                    valveRecord.setAperture(open);
                    if(createTime==null) {
                        valveRecord.setCreateTime(new Date());
                    }else{
                        valveRecord.setCreateTime(createTime);
                    }
                    valveRecordService.insertValveRecord(valveRecord);
                }catch (Exception e){
                    e.printStackTrace();

                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(valveId, "1", "1", "采集阀门数据", "阀门"+valveId+"数据["+datas+"]有误，解析错误"+e.getMessage()));
                }
            }
        };
    }
}
